//
//  HMEmoticonToolbar.swift
//  04-表情键盘
//
//  Created by Admin on 16/9/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum HMEmoticonToolbarType: Int {
    case Recent = 0  // 最近表情
    case Default = 1 // 默认表情
    case Emoji = 2   // Emoji表情
    case Lxh = 3 // 浪小花表情
}

protocol HMEmoticonToolbarDelegate: NSObjectProtocol {
    func didSelectedButton(didSelectedType type: HMEmoticonToolbarType)
}

class HMEmoticonToolbar: UIView {
    
    weak var delegate: HMEmoticonToolbarDelegate?
    
    /// 记录选中的按钮
    private var selectedButton: UIButton?
    
    override func awakeFromNib() {
        // 默认选中第0组
        switchSelectedButton(withSection: 0)
    }

    @IBAction func buttonClick(button: UIButton) {
        switchSelectedButton(button: button)
        
        let type = HMEmoticonToolbarType(rawValue: button.tag)!
        // 调用代理方法
        delegate?.didSelectedButton(didSelectedType: type)
    }
    
    /**
     切换选中按钮
     parameter withSection: 当前collectionView选中的组
     */
    func switchSelectedButton(withSection section: Int) {
        // 获取当前要选中的按钮
        let button = subviews[section] as! UIButton
        
        switchSelectedButton(button: button)
    }
    
    /**
     切换选中按钮
     parameter button: 要选中的按钮
     */
    private func switchSelectedButton(button: UIButton) {
        if selectedButton == button {
            return
        }
        
        // 设置选中的按钮为normal状态
        selectedButton?.isEnabled = true
        
        // 设置传入的按钮为selected状态
        button.isEnabled = false
        
        // 设置选中的按钮为传入的按钮
        selectedButton = button
    }
    
}
